const wxHttpHandler = require('../../utils/wxHttpHandler.js');
const api = require('../../APIConfig/apiConfig.js');
const QQMapWX = require('../../utils/qqmap-wx-jssdk.js')
import * as echarts from '../../ec-canvas/echarts';
var app = getApp();
var qqMapSDK;

var list = []
var list1 = []


Page({
  data: {
    signState: 0, //交接状态
    imgs: [], //本地图片
    images: [], //上传后的图片
    abnormalCause: '', //异常原因
    goodLists: '', //商品列表
    array: ['确认完好', '部分损坏'], //交接状态(0=确认完好1=部分损坏
    warehouseKeeper:'',//仓管员
  },

  onLoad: function (options) {
  },

  onShow: function () {},

  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      signState: parseInt(e.detail.value)
    })
  },

  reasonInput: function (e) {
    this.setData({
      abnormalCause: e.detail.value
    })
  },

  warehouseKeeperInput: function (e) {
    this.setData({
      warehouseKeeper: e.detail.value
    })
  },

  goodsListInput:function(e){
    this.setData({
      goodLists: e.detail.value
    })
  },

  //提交
  submit: function () {
    this.warehousingTransition()
  },

  //选择图片
  chooseImg: function (e) {
    if (this.data.imgs.length >= 4) {
      wx.showToast({
        title: '最多上传4张图片',
      })
      return
    }
    var that = this
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        console.log(res.tempFilePaths[0])
        wx.uploadFile({
          filePath: res.tempFilePaths[0],
          name: 'file',
          url: api.updateImage + wx.getStorageSync('id'),
          success(res) {
            let imgTem = that.data.imgs
            let result = JSON.parse(res.data)
            console.log(result.data)
            imgTem.push(api.baseUrl + result.data)
            that.setData({
              imgs: imgTem
            })
          }
        })
      }
    })
  },

  //入库交接
  warehousingTransition: function () {


    if (this.data.abnormalCause == '') {
      wx.showToast({
        title: '请输入异常原因',
      })
      return
    }

    if (this.data.warehouseKeeper == '') {
      wx.showToast({
        title: '请输入仓管员',
      })
      return
    }

    if (this.data.imgs.length == 0) {
      wx.showToast({
        title: '交接图片不能为空',
      })
      return
    }

    if (this.data.goodLists == '') {
      wx.showToast({
        title: '请输入货品清单',
      })
      return
    }

    let data = {
      abnormalCause: this.data.abnormalCause,
      images: this.data.imgs,
      signState: this.data.signState,
      goodLists: this.data.goodLists,
      warehouseKeeper:wx.getStorageSync('id')
    }
    const that = this
    wx.showLoading()
    wxHttpHandler({
      type: 'get',
      path: api.warehousingTransition,
      data: data,
      fn: function (res) {
        if (res.code == 0) {
          wx.showToast({
            title: '已提交',
          })
          wx.hideLoading()
        } else {
          wx.hideLoading()
          wx.showToast({
            title: res.desc,
            icon: 'none',
            duration: 1500,
            mask: true
          });
        }
      }

    })
  },


  //到货签收
  deliveryOnlineSignFor: function () {

    if (this.data.reason == '') {
      wx.showToast({
        title: '请输入破损原因',
      })
      return
    }

    let data = {
      uid: wx.getStorageSync('id'),
      abnormalCause: this.data.reason,
      images: this.data.imgs,
      signState: this.data.index,
      waybillNumber: this.data.taskNumber,
      signPhone: this.data.receiverPhone,
      goodLists: this.data.goods
    }
    const that = this
    wx.showLoading()
    wxHttpHandler({
      type: 'get',
      path: api.deliverySignFor,
      data: data,
      fn: function (res) {
        if (res.code == 0) {
          wx.showToast({
            title: '已提交',
          })
          wx.hideLoading()
         
        } else {
          wx.hideLoading()
          wx.showToast({
            title: res.desc,
            icon: 'none',
            duration: 1500,
            mask: true
          });
        }
      }

    })
  },


})